﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 创建excel类型的数据源
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("Excel", "CreateDataSource", "1.0")]
    public class CreateEXCELDataSourceAction : BaseAction<CreateEXCELDataSourceParams, CreateEXCELDataSourceResult>
    {
        protected override ActionResult<CreateEXCELDataSourceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateEXCELDataSourceParams opParams)
        {
            int type = 0;
            string valueString = string.Empty;
            string parameterString = string.Format("File,{0},,文件,Sheet,{1},,Sheet页", typeof(FileInfo).FullName, typeof(string).FullName);

            string settings = null;
            if (opParams.FirstRowAsHeader)
            {
                settings = string.Format("FirstRowAsHeader={0}", bool.TrueString);
            }
            else
            {
                settings = string.Format("FirstRowAsHeader={0}", bool.FalseString);
            }

            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult result = dtSerService.CreateDtSource(context.UserId, opParams.DataServiceId, opParams.Name,
                valueString,
                type,
                parameterString,
                opParams.ColumnSettings,
                settings);
            if(result.IsSuccess)
            {
                return new ActionResult<CreateEXCELDataSourceResult>(new CreateEXCELDataSourceResult());
            }
            else
            {
                return new ActionResult<CreateEXCELDataSourceResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class CreateEXCELDataSourceParams
    {
        [Parameter]
        public string Name { get; set; }

        [Parameter]
        public Guid DataServiceId { get; set; }

        [Parameter]
        public string ColumnSettings { get; set; }

        /// <summary>
        /// true：首行数据作为表头，false：首行数据作为数据
        /// </summary>
        public bool FirstRowAsHeader { get; set; }
    }

    public class CreateEXCELDataSourceResult
    {
    }
}
